﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using LPV_POS.In;
using LPV_POS.Baocao;
using LPV_POS.Baocao.Epson;
using LPV_POS.Baocao.Bixolon;
using CrystalDecisions.CrystalReports.Engine;
using DevExpress.XtraExport;
using DevExpress.XtraGrid.Export;

namespace LPV_POS.mh_pos.mh_inbaocao
{
    public partial class frmBaoCao_LaiLo : DevExpress.XtraEditors.XtraForm
    {
        public frmBaoCao_LaiLo()
        {
            InitializeComponent();
        }
        bool iLoadForm = false;
        private void frmBaoCao_NguyenVatLieu_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 1);
            dateTimePicker2.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

            this.Cursor = Cursors.WaitCursor;
            this.ShowData();
            iLoadForm = true;
            this.Cursor = Cursors.Default;
        }

        void ShowData()
        {
            DataTable tb1 = new thuvien().SP_SelectDataTable("sp_BH_CAOCAO_LAILO",
                                                        new string[] { "@ngay_tu", "@ngay_den" },
                                                        new object[] { dateTimePicker1.Value, dateTimePicker2.Value });
            grdData.DataSource = tb1;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void cboNguyenLieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iLoadForm)
                this.ShowData();
        }

        private void dtpThangnam_ValueChanged(object sender, EventArgs e)
        {
            if (iLoadForm)
                this.ShowData();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            string fileName = ShowSaveFileDialog("Excel", "Microsoft Excel|*.xls");
            if (fileName != "")
            {
                ExportTo(new ExportXlsProvider(fileName));
            }

        }
        #region "Export"
        private void ExportTo(IExportProvider provider)
        {
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            this.FindForm().Refresh();
            BaseExportLink link = grvData.CreateExportLink(provider);
            (link as GridViewExportLink).ExpandAll = false;
            link.Progress += new DevExpress.XtraGrid.Export.ProgressEventHandler(Export_Progress);
            link.ExportTo(true);
            provider.Dispose();
            link.Progress -= new DevExpress.XtraGrid.Export.ProgressEventHandler(Export_Progress);

            OpenFile(((DevExpress.XtraExport.ExportCustomProvider)(provider)).FileName);
            Cursor.Current = currentCursor;
        }
        private string ShowSaveFileDialog(string title, string filter)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            string name = "Báo cáo lãi lỗ " + DateTime.Now.ToString("yyyyMMdd");
            int n = name.LastIndexOf(".") + 1;
            if (n > 0) 
                name = name.Substring(n, name.Length - n);
            dlg.Title = "Export To " + title;
            dlg.FileName = name;
            dlg.Filter = filter;
            if (dlg.ShowDialog() == DialogResult.OK)
                return dlg.FileName;
            return "";
        }
        private void OpenFile(string fileName)
        {
            if (XtraMessageBox.Show("Do you want to open this file?", "Export To...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = fileName;
                    process.StartInfo.Verb = "Open";
                    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                    process.Start();
                }
                catch
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(this, "Cannot find an application on your system suitable for openning the file with exported data.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            progressBarControl1.Position = 0;
        }
        private void Export_Progress(object sender, DevExpress.XtraGrid.Export.ProgressEventArgs e)
        {
            if (e.Phase == DevExpress.XtraGrid.Export.ExportPhase.Link)
            {
                progressBarControl1.Position = e.Position;
                this.Update();
            }
        }
        #endregion
        


        #region "   f_splitString6Column"
        string f_splitString6Column(string a, string b, string c, string d, string e, string f)
        {//i1: 39; i2:17
            cpublic ocpublic = new cpublic();
            a = ocpublic.RemoveSign4VietnameseString(a);
            f = ocpublic.RemoveSign4VietnameseString(f);

            string str = "";
            str = a;
            str += new cpublic().Spacestring(20 - a.Length - b.Length);
            str += b;
            str += new cpublic().Spacestring(9 - c.Length);
            str += c;
            str += new cpublic().Spacestring(11 - d.Length);
            str += d;
            str += new cpublic().Spacestring(11 - e.Length);
            str += e;
            str += new cpublic().Spacestring(5 - f.Length);
            str += f;

            return str;
        }
        string Replace_Decimal(string sNumber)
        {

            sNumber = sNumber.Trim().Replace(".0000", "");
            //string[] sn = sNumber.Split(new char[] { '.' });
            //if (sn.Length > 1)
            //{
            //    string sub1 = sn[1];
            //    sNumber = sn[0];
            //    if (new cpublic().IsNumeric(sub1))
            //        if (int.Parse(sub1) > 0)
            //        {
            //            while (sub1.LastIndexOf("0") > 0)
            //                sub1 = sub1.Substring(0, sub1.Length - 1);
            //            sNumber = sNumber + "." + int.Parse(sub1);
            //        }
            //}
            return sNumber;
        }
        #endregion

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (iLoadForm)
            {
                iLoadForm = false;
                if (dateTimePicker1.Value > dateTimePicker2.Value)
                    dateTimePicker2.Value = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day, 23, 59, 59);

                this.ShowData();
                iLoadForm = true;
            }
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            if (iLoadForm)
            {
                iLoadForm = false;
                if (dateTimePicker1.Value > dateTimePicker2.Value)
                    dateTimePicker1.Value = new DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, dateTimePicker2.Value.Day, 0, 0, 1);

                this.ShowData();
                iLoadForm = true;
            }
        }


    }
}